package com.qztech.sdk.ui.activities;

import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

import com.qztech.sdk.base.LoadingDialog;
import com.qztech.utils.YzDebug;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by zhanghb on 2015/9/20.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected abstract int getResourceId();

    protected LoadingDialog mLoadingDialog;

    public abstract boolean hasRegisterEventBus();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getResourceId());
        if (hasRegisterEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
       // ButterKnife.bind(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        //ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        dismissLoading();
        if (hasRegisterEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }



    public void showLoadingDialog(String msg, boolean isCancelable) {
        createDialogIfNull();
        mLoadingDialog.setOnDismissListener(null);
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCancelable(isCancelable);
        mLoadingDialog.setCanceledOnTouchOutside(isCancelable);
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.showLoading();
        }
    }

    protected void createDialogIfNull() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.setOnDismissListener(dialog -> {
               mLoadingDialog = null;
            });
        }
    }

    protected void showSuccess(String errorMsg) {

        createDialogIfNull();
        mLoadingDialog.showSuccess(errorMsg);
        mLoadingDialog.setOnDismissListener(dialog -> {
            readData();
        });
    }
    public void readData() {

    }
    protected void showFail(String errorMsg) {
        createDialogIfNull();
        mLoadingDialog.setOnDismissListener(null);
        mLoadingDialog.showFail(errorMsg);
    }

    public void dismissLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }
}
